package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.VpiInfoRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.VpiInfoRowMapper;
import com.ndp.fb.rdb.model.VpiInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by Mikey.zhaopeng on 2015/3/18 17:46.
 * Email mikey.zhao@ndpmedia.com
 */
@Service
public class VpiInfoRdbServiceImpl extends DaoTemplate implements VpiInfoRdbService {
    @Autowired
    private VpiInfoRowMapper vpiInfoRowMapper;

    @Override
    public Long save(final VpiInfo obj) {
        return saveResultId(obj);
    }

    @Override
    public void save(List<VpiInfo> vpiInfos) {
        batchSaveResultId(vpiInfos);
    }

    @Override
    public void update(VpiInfo vpiInfo) {
        String sql = "UPDATE `vpi_info` SET `fb_connection_object_id` = ?, `connection_object_name` = ?, `platform` = ?, `vpi` = ?, `remark` = ?, `update_time` = ?, WHERE `id` = ?";
        this.getJdbcTemplate().update(sql, vpiInfo.getFbConnectionObjectId(), vpiInfo.getConnectionObjectName(), vpiInfo.getPlatform(), vpiInfo.getVpi(), vpiInfo.getRemark(), new Date(), vpiInfo.getId());
    }

    @Override
    public VpiInfo findByCondition(String userId, Long fbConnectionObjectId, String platform, Integer vpi)
    {
        String sql = "SELECT * FROM vpi_info WHERE 1=1 AND user_id =? AND fb_connection_object_id = ? AND platform = ? AND vpi = ?";
        return findOneDefault(sql, new Object[]{userId, fbConnectionObjectId, platform, vpi}, vpiInfoRowMapper);
    }

    @Override
    public VpiInfo findUnionByCondition(String userId, Long fbConnectionObjectId, String platform, Integer vpi, String type)
    {
        String sql = "SELECT t1.* FROM vpi_info t1 JOIN vpi_country_info t2 ON t1.id = t2.vpi_info_id  WHERE 1=1 AND t1.user_id =? AND t1.fb_connection_object_id = ? AND t1.platform = ? AND t1.vpi = ? AND t2.country_code = ?";
        return findOneDefault(sql, new Object[]{userId, fbConnectionObjectId, platform, vpi, type}, vpiInfoRowMapper);
    }

    @Override
    public void updateUpdateTimeById(Long id, Date time)
    {
        String sql = "UPDATE vpi_info SET update_time = ? WHERE 1=1 AND id = ?";
        Object[] param  = new Object[]{time, id};
        getJdbcTemplate().update(sql, param);
    }

    @Override
    public void deleteVpiInfoAndVpiCountryInfo(String userId, Long fbConnectionObjectId, String platform)
    {
        String sql = "DELETE t1.*, t2.* FROM vpi_info t1, vpi_country_info t2 WHERE t1.id = t2.vpi_info_id AND t1.user_id=? AND t1.fb_connection_object_id=? AND t1.platform=?";
        getJdbcTemplate().update(sql, new Object[]{userId,fbConnectionObjectId,platform});
    }

    @Override
    public List<VpiInfo> findAll() {
        String sql = "SELECT * from vpi_info";
        return this.getJdbcTemplate().query(sql, vpiInfoRowMapper);
    }


    @Override
    public List<VpiInfo> findByFbConnObjIdAndPlatform(Long connObjId, String platform) {
        String sql = "select * from vpi_info where fb_connection_object_id=? and platform=?";
        return this.getJdbcTemplate().query(sql, new Object[]{connObjId, platform}, vpiInfoRowMapper);
    }

}
